package cn.tedu.weibo.controller;

import cn.tedu.weibo.entity.User;
import cn.tedu.weibo.entity.Weibo;
import cn.tedu.weibo.mapper.WeiboMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
public class WeiboController {

    @Autowired(required = false)
    WeiboMapper mapper;

    @RequestMapping("/insert")
    public int insert(Weibo weibo, HttpSession session){
        //得到登陆成功时用户对象
        User user = (User) session.getAttribute("user");
        if(user==null){
            return 2;//代表未登录
        }

        weibo.setCreated(new Date());
        weibo.setUserId(user.getId());
//        System.out.println(user.getId());
        weibo.setNick(user.getNick());
        mapper.insert(weibo);

        return 1;//发布成功
    }

    @RequestMapping("/select")
    public List<Weibo> select(){
        return mapper.select();
    }

    @RequestMapping("/delete")
    public void delete(int id){
        mapper.delete(id);
    }
}
